package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.yxy.YxyQuesKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyQuesKnowRepository;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 老师---布置作业页面--试题类题涉及对的知识点
 * 
 */
@Api(tags = { "V01", "作业系统" })
@CrossOrigin
@RestController
public class WorkAssignQuesKnowController extends YxyApiBaseController {

    @Autowired
    private YxyQuesKnowRepository qKnowRepository;

    @ApiOperation(value = "作业系统老师发布作业,试题类题涉及到的知识点")
    @RequestMapping(value = "/api_yw/manage/tec/homework/workassign/ques/know", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        List<QuesReqModel> quesModeList = reqModel.quesIdList;
        // 封装响应
        List<Integer> knowIdList = convertKnowIds(quesModeList);
        List<String> knowNameList = new ArrayList<String>();
        knowIdList.forEach(knowId -> {
            YxyQuesKnow quesKnow = qKnowRepository.findOne(knowId);
            knowNameList.add(quesKnow.getName());
        });
        RespModel respModel = new RespModel();
        respModel.quesCount = quesModeList.size();
        respModel.knowIdList = knowIdList;
        respModel.knowNameList = knowNameList;
        return respModel;
    }

    /**
     * 转化处理题的知识点
     * 
     * @param quesModeList
     * @return
     */
    private List<Integer> convertKnowIds(List<QuesReqModel> quesModeList) {
        List<Integer> knowIdList = new ArrayList<Integer>();
        for (QuesReqModel quesReqModel : quesModeList) {
            knowIdList.addAll(quesReqModel.know);
        }

        List<Integer> knowIdList2 = new ArrayList<>();
        knowIdList.stream().forEach(p -> {
            if (!knowIdList2.contains(p)) {
                knowIdList2.add(p);
            }
        });
        return knowIdList2;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 本次作业的所选的题
        // public List<String> quesIdList;
        // 本次作业所涉及的题
        public List<QuesReqModel> quesIdList;
    }

    protected static class QuesReqModel {
        // 题的id
        public String quesId;
        // 题涉及的知识点ids
        public List<Integer> know;
    }

    protected static class RespModel extends YxyApiRespModel {

        // 试题数量
        public Integer quesCount;

        // 知识点ids
        public List<Integer> knowIdList;

        // 知识点名称
        public List<String> knowNameList;

    }

}
